//
//  StrategyViewController.m
//  Full_House
//
//  Created by 张忱 on 15-6-3.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "StrategyViewController.h"
#import "StrategyCell.h"
#import "Strategy.h"
#import "StrategyDetailViewController.h"


@interface StrategyViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *strategyArr;
@property (nonatomic, retain) NSMutableDictionary *strategyDic;



@end

@implementation StrategyViewController

- (void)dealloc
{
    [_strategyArr release];
    [_strategyDic release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"装修攻略";
    [self dataHandle];
    [self setupSubView];
    
        
    
}

- (void)setupSubView
{
    //创建TableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[StrategyCell class] forCellReuseIdentifier:@"reusta"];
    [_tableView release];
    
   
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 
    return self.strategyArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"1");
   StrategyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusta"];


    //给cell赋值
    
    Strategy *strategy = [self.strategyArr objectAtIndex:indexPath.row];
    
    
    cell.strategy = strategy;
    
    
    return cell;
    
    
    
}
////点击进入下一界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    StrategyDetailViewController *strategyDetailVC = [[[StrategyDetailViewController alloc] init] autorelease];
    strategyDetailVC.strategy = [self.strategyArr objectAtIndex:indexPath.row];
    
    
    [self.navigationController pushViewController:strategyDetailVC animated:YES];
    
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}



//数据处理
- (void)dataHandle
{
    self.strategyArr = [[NSMutableArray array] init];
    
    NSString *straregyStr =@"http://api.guju.com.cn/v2/ideabook?start=0&count=10";
    NSString *strHandle = [straregyStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strHandle];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        self.strategyDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        self.strategyArr = [Strategy strategyArrWithDicArr:[self.strategyDic objectForKey:@"ideabooks"]];
        //NSLog(@"%@", self.strategyArr);
   [self.tableView reloadData];
    }];
    
    
    
    
    
    
}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
